package com.nowcoder.community.controller;

import com.alibaba.fastjson.JSONObject;
import com.nowcoder.community.contant.Topic;
import com.nowcoder.community.entity.Message;
import com.nowcoder.community.entity.Page;
import com.nowcoder.community.entity.User;
import com.nowcoder.community.service.MessageService;
import com.nowcoder.community.service.UserService;
import com.nowcoder.community.utils.HostHolder;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;

import java.util.HashMap;
import java.util.LinkedList;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

/*
* 消息 - 系统通知
* */
@Controller
@RequestMapping("/notice")
public class NoticeController {

    @Autowired
    private MessageService messageService;

    @Autowired
    private UserService userService;

    @Autowired
    private HostHolder hostHolder;

    @GetMapping("/list")
    public String notice(Model model){
        User user = hostHolder.getUser();
        model.addAttribute("unreadLetterCount", messageService.countUnreadMessages(user.getId()));
        model.addAttribute("unreadNoticeCount", messageService.countUnreadNotices(user.getId(), null));
        // 评论通知
        model.addAttribute("commentNotice", getLatestNotice(user.getId(), Topic.COMMENT));
        // 赞通知
        model.addAttribute("likeNotice",getLatestNotice(user.getId(), Topic.LIKE));
        // 关注通知
        model.addAttribute("followNotice", getLatestNotice(user.getId(), Topic.FOLLOW));
        return "/site/notice";
    }

    @GetMapping("/detail/{topic}")
    public String noticeDetail(@PathVariable("topic") String topic, Page page, Model model){
        User user = hostHolder.getUser();
        page.setRows(messageService.countNotices(user.getId(), topic));
        page.setPath("/notice/detail/" + topic);
        page.setLimit(5);

        List<Message> notices = messageService.findNotices(user.getId(), topic, page.getOffset(), page.getLimit());
        List<Map<String, Object>> noticesOV = new LinkedList<>();
        for (Message notice: notices){
            Map<String, Object> map = new HashMap<>();
            map.put("notice", notice);
            map.put("fromUser", userService.findUserById(notice.getFromId()));
            HashMap<String, Object> content = JSONObject.parseObject(notice.getContent(), HashMap.class);
            content.forEach((k, v)->map.put(k,v));
            map.put("user", userService.findUserById((int)content.get("userId")));
            noticesOV.add(map);
        }
        // 标记通知为已读
        messageService.updateMessagesStatus(notices.stream().map(notice->notice.getId()).collect(Collectors.toList()), 1);
        model.addAttribute("notices", noticesOV);
        return "/site/notice-detail";
    }

    private Map<String, Object> getLatestNotice(int userId, String topic){
        Map<String, Object> map = new HashMap<>();
        map.put("unreadCount", messageService.countUnreadNotices(userId,topic));
        map.put("noticeCount", messageService.countNotices(userId, topic));
        Message message = messageService.findLatestNotice(userId, topic);
        map.put("message", message);
        if (message != null){
            Map<String, Object> content = JSONObject.parseObject(message.getContent(), HashMap.class);
            content.forEach((k, v) -> map.put(k, v));
            map.put("user", userService.findUserById((Integer) content.get("userId")));
        }
        return map;
    }
}

